package com.xingkeduo.controller;

import com.xingkeduo.model.param.CheckWifiParam;
import com.xingkeduo.model.param.ClockInStatusParam;
import com.xingkeduo.model.param.TStaffClockInParam;
import com.xingkeduo.model.po.TStoreWifiCheck;
import com.xingkeduo.model.vo.StaffAttendanceStatusVo;
import com.xingkeduo.model.vo.StaffAttendanceVo;
import com.xingkeduo.service.StaffClockInService;
import com.xingkeduo.service.StoreWifiCheckService;
import com.xingkeduo.util.tool.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description TODO
 * @Date 2019/3/26 10:18
 * @Created 情绪十分不稳定的Shadow
 */

@RestController
@RequestMapping("/staff/clockIn")
public class StaffClockInController {

    private static Logger log = LoggerFactory.getLogger(StaffClockInController.class);

    @Autowired
    private StaffClockInService service;

    @Autowired
    private StoreWifiCheckService wifiCheckService;


    /**
     * @description  打卡逻辑
     * @Author 情绪极其不稳定的Shadow
     * @Param [staffClockIn, wifiName, staffAttendanceId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @RequestMapping(value = "/saveStaffClockIn", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public ResponseMessage saveStaffClockIn(@RequestBody TStaffClockInParam param){
        log.info("打卡param--->>>"+param.toString());
        /** 数据校验 **/
        if(StringUtils.isEmpty(param.getStaffAttendanceId())){
            return ResponseMessage.error("缺少发型师考勤id",500);
        }

        if(StringUtils.isEmpty(param.getStaffId())
            || StringUtils.isEmpty(param.getStaffName())){
            return ResponseMessage.error("缺少发型师id or 缺少发型师name",500);
        }
        if(StringUtils.isEmpty(param.getStoreId())
            || StringUtils.isEmpty(param.getStoreName())){
            return ResponseMessage.error("缺少店铺id or 缺少店铺name", 500);
        }
        if(null == param.getType() || 0 == param.getType()){
            return ResponseMessage.error("为提示打卡类型",500);
        }
        //TODO 为校验打卡时间date是否为当天日期
        if(!(param.getStaffId() == 9459L)){
            boolean flag;
            //判断是否是代打卡
            if(!param.getReplaceClockIn()){
                /** 校验参数 **/
                if(StringUtils.isEmpty(param.getWifiName())){
                    return ResponseMessage.error("缺少店铺wifiName",500);
                }
                /** wifi校验 **/
                flag = wifiCheckService.checkWifi(param.getWifiName(), param.getStoreId());
                if(!flag){
                    return ResponseMessage.error("此wifi不是店铺打卡wifi", 500);
                }
            }
        }
        int res = service.saveStaffClockIn(param);
        if (res > 0) {
            Date realityTime = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            return ResponseMessage.ok(dateFormat.format(realityTime),"打卡成功");
        }
        return ResponseMessage.error("打卡失败,请联系管理员", 500);
    }

    /**
     * @description  根据发型师id 店铺id 查询发型师打卡状态
     * @Author 情绪极其不稳定的Shadow
     * @Param [staffId, storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStaffClockInStatus")
    public ResponseMessage getStaffClockInStatus(Long staffId){
        if(StringUtils.isEmpty(staffId)){
            return ResponseMessage.error("缺少发型师id",500);
        }
        StaffAttendanceVo staffAttendanceVo = service.getStaffClockInStatus(staffId);
//        if (staffAttendance != null){
            return ResponseMessage.ok(staffAttendanceVo/*,"已打上班卡"*/);
//        }
//        return ResponseMessage.ok(staffAttendance,"未打卡 or 已打下班卡");
    }
    
    /**
     * @description  校验店铺wifi
     * @Author 情绪极其不稳定的Shadow
     * @Param [param]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @PostMapping("/saveOrUpdateWifiName")
    public ResponseMessage saveOrUpdateWifiName(@RequestBody CheckWifiParam param){
        if(StringUtils.isEmpty(param.getWifiName())){
            return ResponseMessage.error("缺少wifiName", 500);
        }
        if (StringUtils.isEmpty(param.getStoreId())){
            return ResponseMessage.error("缺少店铺id", 500);
        }
        if(StringUtils.isEmpty(param.getManagerId())){
            return ResponseMessage.error("缺少操作人id", 500);
        }
        int res = wifiCheckService.saveOrUpdateWifiName(param);
        if(res > 0){
            return ResponseMessage.ok(wifiCheckService.getStoreWifi(param.getStoreId()),"校验成功");
        }
        return ResponseMessage.error("操作异常,参数并未有任何修改", 500);
    }

    /**
     * @description  查询店铺是否校验wifi
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStoreWifi")
    public ResponseMessage getStoreWifi(Long storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺id", 500);
        }
        TStoreWifiCheck storeWifi = wifiCheckService.getStoreWifi(storeId);
        return ResponseMessage.ok(storeWifi);
    }
    
    /**
     * @description 打卡前查看考勤状态
     * @Author 情绪极其不稳定的Shadow
     * @Param [param]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @PostMapping("/getClockInStatus")
    public ResponseMessage getClockInStatus(@RequestBody ClockInStatusParam param){
        if(StringUtils.isEmpty(param.getStaffAttendanceId())){
            return ResponseMessage.error("缺少发型师考勤id",500);
        }
        if(null == param.getType() || 0 == param.getType()){
            return ResponseMessage.error("为提示打卡类型",500);
        }
        StaffAttendanceStatusVo clockInStatus = service.getClockInStatus(param);
        if(null == clockInStatus){
            return ResponseMessage.error("未找到对应考勤记录,请检查考勤ID or 联系系统管理员", 500);
        }
        return ResponseMessage.ok(clockInStatus);
    }


}
